<?php
/*
	挪车卡订单列表
	获取所代理地区的订单！
	如何根据代理地区来获取挪车卡订单！
*/
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','query'=>'status=0','title'=>'待付款'),
	array('op'=>'list','query'=>'status=1','title'=>'待发货'),
	array('op'=>'list','query'=>'status=2','title'=>'待收货'),
	array('op'=>'list','query'=>'status=3','title'=>'已签收'),
	array('op'=>'list','query'=>'status=4','title'=>'已取消'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['agent_id']=$_SESSION['uid'];
	//订单状态
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	//所属会员
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}else{
		$mids=$this->getall('member',array('agent_id'=>$_SESSION['uid']),array('mid'),'mid');
		$mids=array_keys($mids);
		if($mids){
			$params['mid']=$mids;
		}else{
			$params['mid']=0;//设置为0，从而查不到任何订单
		}
	}
	//订单号
	if($_RQ['ordersn']){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	//下单时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','mid','ordersn','status','name','pcd','address','ship_name','ship_order','createtime','sendtime');
	$list=pdo_getslice('jy_move_agentcar_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			
			$item['pcd']=json_decode($item['pcd'],true);
			$item['address']=$item['pcd']['province'].$item['pcd']['city'].$item['pcd']['district'].$item['address'];
			$item['wuliu']=$item['ship_name'].'：'.$item['ship_order'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'wuliu','title'=>'物流','type'=>'string'),
			array('name'=>'sendtime','title'=>'发货时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'ship','title'=>'发货','show'=>array('status'=>1)),
			array('type'=>'href','op'=>'detail','title'=>'查看详情'),
		)),
		'search'=>array(
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),
			//array('name'=>'mid','title'=>'所属会员','type'=>'choose','action'=>'member.get_member','plugin'=>'core'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time_range'),
		),
	);
	$return['status']=$this->order_status();
	$return['htmls']=$htmls;
	exi($return);
}
//发货
if($_RQ['op']=='ship'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$update=array(
			'ship_name'=>trim($post['ship_name']),
			'ship_order'=>trim($post['ship_order']),
			'status'=>2,
			'sendtime'=>TIMESTAMP,
		);
		$this->update('order',$update,array('id'=>$id));
		exi('发货成功！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要发货的订单！','error');
	}
	$item=pdo_get('jy_move_agentcar_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','status'));
	if(!$item){
		exi('指定订单不存在！','error');
	}
	if($item['status']!=1){
		exi('指定订单不满足发货条件！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置物流信息',
		'edit'=>array(
			array('type'=>'string','name'=>'ship_name','title'=>'快递公司'),
			array('type'=>'string','name'=>'ship_order','title'=>'快递单号'),
		),
	);
	exi($return);
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要查看的订单！','error');
	}
	$item=pdo_get('jy_move_agentcar_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$statuss=$this->order_status();
	$item['status']=$statuss[$item['status']];
	$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
	//$item['pay_type']=$pay_types[$item['pay_type']];
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'订单详情',
		'look'=>array(
			array('type'=>'string','name'=>'ordersn','title'=>'订单号'),
			array('type'=>'string','name'=>'price','title'=>'订单金额'),
			array('type'=>'string','name'=>'status','title'=>'订单状态'),
			//array('type'=>'string','name'=>'pay_type','title'=>'支付方式'),
			array('type'=>'string','name'=>'pay_id','title'=>'系统支付单号'),
			array('type'=>'string','name'=>'createtime','title'=>'订单创建时间'),
			array('type'=>'string','name'=>'ship_name','title'=>'收货人'),
			array('type'=>'string','name'=>'ship_mobile','title'=>'收货人电话'),
			array('type'=>'string','name'=>'ship_address','title'=>'收货人地址'),
			array('type'=>'string','name'=>'ship_type','title'=>'物流公司'),
			array('type'=>'string','name'=>'ship_order','title'=>'快递单号'),
		),
	);
	exi($return);
}